package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.domain.PBrand;
import com.mk.cloud.common.domain.PProduct;
import com.mk.cloud.common.enums.ErrorCode;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.exception.UserException;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.DateUtils;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.*;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("brand")
public class PBrandController extends CookieService {

    @Inject
    private PBrand.Mapper pBrandMapper;
    @Inject
    private PProduct.Mapper pProductMapper;


    @Description("品牌--品牌列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<PBrand> pBrandList(
            @Description("筛选条件-品牌名称") String brname,
            @Description("筛选条件-品牌所属公司") String company,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<PBrand> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        map.put("brname", brname);
        map.put("company", company);
        map.put("coid", me.getCoid());
        query.setParas(map);
        //此处的sql是三个sql
        pBrandMapper.getSQLManager().pageQuery("pBrand.select_brand_page_list", PBrand.class, query);
        return query;
    }

    @Description("品牌---品牌列表")
    @With(ICLogin.class)
    @GetAction("brandList.json")
    public List<PBrand> brandList() {
        PBrand brand = new PBrand();
        brand.setCoid(me.getCoid());
        return pBrandMapper.template(brand);
    }

    @Description("品牌--品牌详情")
    @With(ICLogin.class)
    @GetAction("get.json")
    public PBrand getPBrand(@Description("品牌id") String brid) {
        if (S.isEmpty(brid)) {
            return new PBrand();
        }
        return pBrandMapper.single(brid);
    }

    @Description("品牌--品牌保存")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("save.json")
    public String savePBrand(@Description("品牌对象") PBrand brand) {
        brand.setCoid(me.getCoid());
        if (S.isNotBlank(brand.getBrid())) {
            pBrandMapper.updateTemplateById(brand);
            return brand.getBrid();
        } else {
            String brid = S2.UUID();
            brand.setBrid(brid);
            pBrandMapper.insert(brand);
            return brid;
        }
    }

    @Description("品牌--品牌删除")
    @With({ICLogin.class, ITransacte.class})
    @DeleteAction("{brid}/del.json")
    public Integer delPBrand(@Description("品牌id") String brid) {
        PProduct product = new PProduct();
        product.setBrid(brid);
        long count = pProductMapper.templateCount(product);
        UserException.throwIf(count>0, ErrorCode.E202004);
        return pBrandMapper.deleteById(brid);
    }


}
